<?php

/**
 * forum actions.
 *
 * @package    sfwixet
 * @subpackage forum
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class forumActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->echo = $request->getParameter("sEcho") + 1;
  }
  
  public function executeGetTopics(sfWebRequest $request)
  {
  	$json = array();
  	$json['idList'] = array(1,2,3);
  	$json['sEcho'] = $request->getParameter("sEcho") + 1;
  	$json['iTotalRecords'] = 1;
  	$json['iTotalDisplayRecords'] = 1;
  	$data = array();
  	$data[] = array("Mola","Junajo",10,21,'el 5 de ocutre de 1989 por <a href="aaa">Pepito</a>');
  	
  	$json['aaData'] = $data;

  	echo json_encode($json);
  	return sfView::NONE;
  }
  
  public function executeGetMessages(sfWebRequest $request){
  	
  }
  
  public function executeCreate(sfWebRequest $request){
  	try{
	  	if($this->getUser()->hasCredential("site")){
	  		if(($name=$request->getParameter("name"))){
	  			$forum = new forum();
	  			$forum->setUser($this->getUser()->getGuardUser());
	  			$forum->setName($name);
	  			$forum->save();
	  			echo json_encode(array("error"=>false,"id"=>$forum->getForumId()));
	  		}else throw new Exception("Please provide a forum name");
	  	}else throw new Exception("Only sites can create forums");
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
  	return sfView::NONE;
  }
  
  public function executeCreateBoard(sfWebRequest $request){
  	try{
	  	if($this->getUser()->hasCredential("site")){
	  		if(($name=$request->getParameter("name")) && ($id=$request->getParameter("id"))){
	  			$forum = Doctrine::getTable("forum")->find($id);
	  			if($forum->getUserId() == $this->getUser()->getId()){
		  			$board = new forumBoard();
		  			$board->setTitle($name);
		  			$board->setForum($forum);
		  			$board->save();
		  			echo json_encode(array("error"=>false,"id"=>$board->getForumBoardId()));
	  			}else throw new Exception("You cannot modify this forum");
	  			
	  		}else throw new Exception("Please provide a board name and forum id");
	  	}else throw new Exception("Only sites can create boards");
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
  	return sfView::NONE;
  }
  
  public function executeRemove(sfWebRequest $request){
  	try{
	  	if($this->getUser()->hasCredential("site")){
	  		if(($id=$request->getParameter("id"))){
	  			$forum = Doctrine::getTable("forum")->find($id);
	  			if($forum->getUserId() == $this->getUser()->getId()){
		  			$forum->delete();
		  			echo json_encode(array("error"=>false));
	  			}else throw new Exception("You cannot modify this forum");
	  			
	  		}else throw new Exception("Please provide a forum id");
	  	}else throw new Exception("Only sites can remove forums");
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
  	return sfView::NONE;
  }
  
  public function executeRemoveBoard(sfWebRequest $request){
  	try{
	  	if($this->getUser()->hasCredential("site")){
	  		if(($id=$request->getParameter("id"))){
	  			$board = Doctrine::getTable("forumBoard")->find($id);
	  			if($board->getForum()->getUserId() == $this->getUser()->getId()){
		  			$board->delete();
		  			echo json_encode(array("error"=>false));
	  			}else throw new Exception("You cannot modify this board");
	  			
	  		}else throw new Exception("Please provide a board id");
	  	}else throw new Exception("Only sites can remove boards");
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
  	return sfView::NONE;
  }
}
